package com.cscj.android.utils

import android.app.Activity
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Rect
import android.os.Build
import android.os.Handler
import android.os.Looper
import android.util.Base64
import android.view.PixelCopy
import android.view.View
import java.io.ByteArrayOutputStream

object BitmapUtils {

    private fun bitmap2ByteArray(bitmap: Bitmap): ByteArray {
        val bos = ByteArrayOutputStream()
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, bos)
        return bos.toByteArray()
    }

    private fun byteArray2Base64(bytes: ByteArray): String {
        return Base64.encodeToString(bytes, Base64.DEFAULT)
    }

    private fun base64ToByteArray(content: String): ByteArray {
        return if (content.contains(",")) {
            val realContent = content.split(",").last()
            Base64.decode(realContent, Base64.DEFAULT)
        } else {
            Base64.decode(content, Base64.DEFAULT)
        }
    }

    private fun byteArrayToBitmap(bytes: ByteArray): Bitmap {
        return BitmapFactory.decodeByteArray(bytes, 0, bytes.size)
    }

    fun base64ToBitmap(content: String): Bitmap {
        return byteArrayToBitmap(base64ToByteArray(content))
    }

    fun bitmapToBase64(bitmap: Bitmap): String {
        return byteArray2Base64(bitmap2ByteArray(bitmap))
    }

    fun Bitmap.asBase64String(): String {
        return bitmapToBase64(this)
    }

    fun String.asBitmap(): Bitmap {
        return base64ToBitmap(this)
    }

    fun captureView(activity: Activity, view: View, callback: (Bitmap) -> Unit) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            activity.window?.let { window ->
                val bitmap = Bitmap.createBitmap(view.width, view.height, Bitmap.Config.ARGB_8888)
                val location = IntArray(2)
                view.getLocationInWindow(location)

                try {
                    PixelCopy.request(
                        window, Rect(
                            location[0],
                            location[1],
                            location[0] + view.width,
                            location[1] + view.height
                        ), bitmap, { copyResult ->
                            if (copyResult == PixelCopy.SUCCESS) {
                                callback(bitmap)
                            } else {
                                LOG.e("BitmapUtils captureView failed.")
                            }
                        }, Handler(Looper.getMainLooper())
                    )
                } catch (e: Exception) {
                    LOG.e("BitmapUtils captureView exception", e)
                }

            }
        } else {
            val screenView = view.rootView
            screenView.isDrawingCacheEnabled = true
            val bitmap = Bitmap.createBitmap(screenView.drawingCache)
            screenView.isDrawingCacheEnabled = false
            callback(bitmap)
        }
    }

}